package ysq.xyg.host.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.api.aliapi.AliSmsApi;
import ysq.xyg.base.extend.ehcache.EhCacheService;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.base.util.ProProperties;
import ysq.xyg.host.dao.HostReportDao;
import ysq.xyg.host.model.dto.HostReportDto;
import ysq.xyg.host.model.dto.HostReportExportDto;
import ysq.xyg.host.model.po.HostReport;
import ysq.xyg.host.model.vo.HostReportSearchVo;
import ysq.xyg.host.model.vo.HostReportVo;
import ysq.xyg.host.service.HostReportService;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebConfig;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.service.WebMemberService;

import java.util.Date;
import java.util.List;

/**
 * 房产-楼盘报备service实现
 *
 * @author congcong tao
 * @date 2019-12-11 14:59:49
 */
@Service("hostReportService")
public class HostReportServiceImpl extends BaseServiceImpl<HostReport> implements HostReportService {

    @Autowired
    private EhCacheService ehCacheService;
    @Autowired
    private WebMemberService webMemberService;
    @Autowired
    private HostReportDao hostReportDao;

    @Autowired
    public void setBaseDao(HostReportDao hostReportDao) {
        this.baseDao = hostReportDao;
    }

    /**
     * 分页查询
     *
     * @param hostReportVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 14:59:49
     */
    @Override
    public ReturnEntity page(HostReportVo hostReportVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" create_time desc");
        List<HostReport> list = hostReportDao.selectHostReportList(hostReportVo);
        PageInfo<HostReport> page = new PageInfo<>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    @Override
    public ReturnEntity myHostReportPage(HostReportSearchVo reportVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<HostReportDto> list = hostReportDao.selectMyHostReportList(reportVo);
        PageInfo<HostReportDto> page = new PageInfo<>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostReportVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 14:59:49
     */
    @Override
    public ReturnEntity addOrUpdate(HostReportVo hostReportVo, SysUser currentUser) {
        boolean isUpate = true;
        HostReport hostReport;
        if (StrUtil.isEmpty(hostReportVo.getId())) {
            hostReport = new HostReport(currentUser);
            isUpate = false;
        } else {
            hostReport = selectByPrimaryKey(hostReportVo.getId());
            if (ObjectUtil.isNull(hostReport)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostReport.setUpdateTime(new Date());
        }
        hostReport.setItemId(hostReportVo.getItemId());
        hostReport.setMemberId(hostReportVo.getMemberId());
        hostReport.setName(hostReportVo.getName());
        hostReport.setSex(hostReportVo.getSex());
        hostReport.setPhone(hostReportVo.getPhone());
        hostReport.setRecommendName(hostReportVo.getRecommendName());
        hostReport.setRecommendPhone(hostReportVo.getRecommendPhone());
        hostReport.setUseTime(DateUtil.parseDateTime(hostReportVo.getUseTime()));
        hostReport.setValidTime(DateUtil.parseDateTime(hostReportVo.getValidTime()));
        hostReport.setRemarks(hostReportVo.getRemarks());
        hostReport.setReportState(hostReportVo.getReportState());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostReport);
        } else {
            num = insert(hostReport);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 14:59:49
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostReport hostReport = selectByPrimaryKey(id);
        if (hostReport != null) {
            hostReport.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostReport.setUpdateTime(new Date());
            int num = updateByPrimaryKey(hostReport);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostReportVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 14:59:49
     */
    @Override
    public List<HostReport> acceptList(HostReportVo hostReportVo){
        Example example = new Example(HostReport.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostReportVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostReportVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return hostReportDao.selectByExample(example);
    }

    @Override
    public ReturnEntity saveHostReport(HostReportVo hostReportVo) {
        // 以手机号确认该用户是否已报备过，在多少时间内有效
        // 推荐人如果是空的话就是本人，从member表中获取本人信息，需要状态判断
        HostReport hostReport = new HostReport();
        hostReport.setId(BaseUtil.uId());
        hostReport.setItemId(hostReportVo.getItemId());
        hostReport.setMemberId(hostReportVo.getMemberId());
        hostReport.setName(hostReportVo.getName());
        hostReport.setSex(hostReportVo.getSex());
        hostReport.setPhone(hostReportVo.getPhone());
        if (StringUtils.isEmpty(hostReportVo.getRecommendName()) && StringUtils.isEmpty(hostReportVo.getRecommendPhone())) {
            WebMember webMember = webMemberService.selectByPrimaryKey(hostReportVo.getMemberId());
            hostReport.setIsRecommend(BaseConstant.HOST_REPORT_MEMBER_YES);
            hostReport.setRecommendName(webMember.getName());
            hostReport.setRecommendPhone(webMember.getPhone());
        } else {
            hostReport.setIsRecommend(BaseConstant.HOST_REPORT_MEMBER_NO);
            hostReport.setRecommendName(hostReportVo.getRecommendName());
            hostReport.setRecommendPhone(hostReportVo.getRecommendPhone());
        }
        hostReport.setRemarks(hostReportVo.getRemarks());
        hostReport.setReportState(BaseConstant.HOST_REPORT_STATE_ONE);
        hostReport.setUseTime(DateUtil.parseDate(hostReportVo.getUseTime()));
        hostReport.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        hostReport.setCreateTime(new Date());
        hostReport.setUpdateTime(hostReport.getCreateTime());
        int num = insert(hostReport);
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        // 需要发送短信提醒
        String sendSmsFlag = ProProperties.getInstance().getSendSmsFlag();
        if (StringUtils.equals("true", sendSmsFlag)) {
            WebConfig webConfig = ehCacheService.selectWebConfigByAccountId(hostReportVo.getAccountId());
            if (webConfig != null && StringUtils.isNotEmpty(webConfig.getHostManagePhone())) {
                String param = "{\"code\":\"000000\"}";
                AliSmsApi.send(webConfig.getHostManagePhone(), AliSmsApi.TEMPLATE_CODE, param);
            }
        }
        return new ReturnEntity();
    }

    @Override
    public List<HostReportExportDto> selectExportEntity(HostReportVo hostReportVo) {
        return hostReportDao.selectExportEntity(hostReportVo);
    }
}
